package com.example.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/request")
public class SpringStudy {
    @RequestMapping("/hello")
    public String sayHello(){
        return "welcome to Spring";
    }
    @RequestMapping("hello2")
    public String sayHello2(){
        return "welcome to Spring2";
    }
    @RequestMapping(value = "hello3",method ={RequestMethod.GET})
    public String sayHello3(){
        return "welcome to Spring3";
    }
    //用spring框架来接收http请求传来的参数
    @RequestMapping("/r1")
    public String r1(String name){
        return "name: "+name;
    }
    @RequestMapping("/r2")
    public String r2(String name,Integer age){
        return "name: "+name+", age:" +age;
    }
    @RequestMapping("/r3")
    public String r3(String name,Integer age,int a){
        return "name: "+name+", age:" +age+", a："+a;
    }
    @RequestMapping("/r4")
    public String r4(@RequestParam(required = false) String name){
        return "name: "+name;
    }
    @RequestMapping("/r5")
    public String r5(@RequestParam(value = "name",required = false) String myName){
        return "name: "+myName;
    }
    @RequestMapping("/r6")
    public String r6(Person person){
        return "person:  "+person;
    }
    //spring进行接受对象依赖的是内部的属性所以对整个对象的名字没有影响
    @RequestMapping("/r7")
    public String r7( Person student){
        return "person: "+student;
    }
    @RequestMapping("/r8")
    public String r8(Integer[] arr){
        if(arr==null) return  Arrays.toString(arr);
        return "arr.length"+arr.length+"\n"+ Arrays.toString(arr);
    }
    @RequestMapping("/r9")
    public String r9(@RequestParam("arr") String[] array){
        if(array==null) return  Arrays.toString(array);
        return "arr.length: "+array.length+"\n"+ Arrays.toString(array);
    }
    @RequestMapping("/r10")
    public String r10(@RequestParam  List<String> list){
        return "list: "+list;
    }
    @RequestMapping("/r11")
    public String r11(@RequestParam  List<Integer> list){
        return "list: "+list;
    }
    @RequestMapping("/r12")
    public String r12(@RequestParam(value = "list",required = false)  List<String> arr){
        if(arr==null) return "list: "+arr;
        return "list: "+arr.size()+"/n"+arr;
    }
    //把请求正文转换成对象（注意围绕着对象来使用）
    @RequestMapping("/r13")
    public String r13(@RequestBody(required = false) Person[] person){
        return "people: "+Arrays.toString(person);
    }
    @RequestMapping("/r14")
    public String r14(@RequestBody Person person){
        return "people: "+person;
    }

    //获取url中的参数，是一个必传参数
//    @RequestMapping("/article/{article_id}")
//    public String getUrl_id(@PathVariable String article_id){
//        return "article1： "+article_id;
//    }

    @RequestMapping("/article/{article_id2}")
    public String getUrl_id2(@PathVariable("article_id2") Integer article){
        return "article2  ： "+article;
    }
    @RequestMapping("/article/{article_id2}/{name}")
    public String getUrl_id3(@PathVariable("article_id2") Integer article,@PathVariable String name){
        return "article_id2  ： "+article+",name: "+name;
    }

    //接受一个文件
    @RequestMapping("/r15")
    public String r15(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        file.transferTo(new File("D:\\javaku\\java\\html\\pic2/"+originalFilename));
        return "接收到的文件名称为： "+originalFilename;
    }

    @RequestMapping("/r16")
    public String r16(@RequestPart("file") MultipartFile myFile) throws IOException {
        String originalFilename = myFile.getOriginalFilename();
        myFile.transferTo(new File("D:\\javaku\\java\\html\\pic2/"+originalFilename));
        return "接收到文件的名称为"+originalFilename;
    }

    @RequestMapping("/r17")
    public String r17(@RequestParam("file") MultipartFile myFile) throws IOException {
        String originalFilename = myFile.getOriginalFilename();
        myFile.transferTo(new File("D:\\javaku\\java\\html\\pic2/"+originalFilename));
        return "接收到文件的名称为"+originalFilename;
    }
    //cookies and session
    @RequestMapping("/getCookies")
    public String getCookies(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        StringBuilder stringBuilder=new StringBuilder();
        for (Cookie c:cookies
             ) {
            stringBuilder.append(c.getName()+":"+c.getValue()+"\n");
        }
        return "cookies:  "+stringBuilder;
    }
    @RequestMapping("/setSession")
    public String setSession_ID(HttpServletRequest request,String name,Integer id){
        HttpSession session = request.getSession();
        session.setAttribute("id",id);
        session.setAttribute("name",name);
        return "Session设置成 功";
    }
    @RequestMapping("/getSession")
    public String getSession_ID(HttpServletRequest request){
        HttpSession session = request.getSession(true);
         Object name = session.getAttribute("name");
        Object id = session.getAttribute("id");
        return "name: "+name+", id: "+id;
    }
    @RequestMapping("/set")
    public String setSession(HttpSession session,String name,int id){
        session.setAttribute("name",name);
        session.setAttribute("id",id);
        return "Session设置成功";
    }
    @RequestMapping("/get")
    public String getSession(HttpSession session){
        Object name = session.getAttribute("name");
        Object id = session.getAttribute("id");
        return "name: "+name+", id: "+id;
    }
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String header = request.getHeader("User-Agent");
        return "header: "+header;
    }
    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader(value ="User-Agent" ,required = false) String username){
        return "header: "+username;
    }





}
